<?php

class Show_model extends CI_Model{
    function __construct(){
        parent::__construct();
    }

	/*
	* string $id 用户uid
	* return array("userTags"=>当前用户的兴趣爱好,"agencyTag=>",其他机构标签,"diff"=>用户上游所有用户Tag)
	*/
	function getDiffuseByid($id){
		$friends = $this->GetFriendsByid($id);
		$fids = array();
		foreach($friends as $key=>$value){
			array_push($fids,$value["fid"]);
		}
		$ftags = $this->getTagByIds($fids);
		$myficm = $this->getIncommonByid($id);
		$userTags = "其他 ";
		$agencyTag = "";
		$result = array();
		foreach($friends as $key=>$friend){
			$tempUserTag = "";
			$tempUserAgencyTag = ""; 
			$MyCen = false;
			if(!isset($myficm[$friend["fid"]])){
				$tempUserTag = '其他';
				foreach($ftags as $k => $tag){
					if($tag["uid"]===$friend["fid"]){
						$tagsplit = explode("/",$tag["tag"]);
						$tempUserTag= $tagsplit["0"];
						$MyCen = true;
						if(!strstr($userTags,$tempUserTag)){
							$userTags .= $tempUserTag." ";
						}
						break;
					}
				}
			}else{
				foreach($myficm[$friend["fid"]] as $key=>$value){
						foreach($ftags as $k => $tag){
							if($tag["uid"]===$friend["fid"]){
								$tagsplit = explode("/",$tag["tag"]);
								$tempUserTag= $tagsplit["0"];
								$MyCen = true;
								if(!strstr($userTags,$tempUserTag)){
									$userTags.= $tempUserTag." ";
								}
								break;
							}
							if($value["fid"]!=$tag['uid'])
								continue;
							if($tag["type"]==="1"){
								$tagsplit = explode("/",$tag["tag"]);
								$utag= $tagsplit["0"];
								if(strstr($tempUserTag,$utag)==false){
									$tempUserTag.= $utag." ";
								}
								if(!strstr($userTags,$utag)){
									$userTags.= $utag." ";
								}
							}else{
								$tagsplit = explode("/",$tag["tag"]);
								$utag= $tagsplit["0"];
								if(!strstr($tempUserAgencyTag,$utag)){
									$tempUserAgencyTag.= $utag." ";
								}
								if(!strstr($agencyTag,$utag)){
									$agencyTag.= $utag." ";
								}						
							}

						}
						if($MyCen == true){
							break;
						}
				}//whosyourdaddy
			}
			$friend["tag"] = $tempUserTag;
			$friend["agtag"] = $tempUserAgencyTag;
			$result[] = $friend;
		}
		return array("userTags"=>$userTags,"agencyTag=>",$agencyTag,"diff"=>$result);
	}

	/*
	* string $uid 用户UID
	* return array 用户关系
	*/
	function getIncommonByid($uid){
		$this->db->from("wb_twoway_common");
		$this->db->where("uid",$uid);
		$this->db->order_by("oid", "DESC"); 
		$icfs = $this->db->get()->result_array();
		$tmpoid = null;
		$idFlagIncm = array();
		
		foreach($icfs as $key=>$value){
			if(!isset($idFlagIncm[$value["oid"]])){
				$idFlagIncm[$value["oid"]] = array();
			}

			$idFlagIncm[$value["oid"]][] =$value;
		}
		return $idFlagIncm;
	}

	/*
	* string $id	用户UID
	* return array  获取好友
	*/
	function GetFriendsByid($id){
		$this->db->from(" wb_twoway_relationship");
		$this->db->select("fid,fname,relation");
		$this->db->where('uid',$id);
		$query = $this->db->get();
		return $query->result_array();
	}

	/*
	* array()  $ids   获取好友所有id
	* return   array  获取好友tag
	*/

	function getTagByIds($ids = array()){
		$this->db->from("wb_celebrity");
		foreach($ids as $key=>$value){
			$this->db->or_where("uid",$value);
		}
		$query = $this->db->get();
		return $query->result_array();
	}

	/*
	*string $uid     用户UID
	*array  $top30   排除强关系（influence 前30）
	*return array()  用户前4个兴趣（每个兴趣5个人）
	*/
	function getInterest($uid,$top30){
		$difftags = $this->getDiffuseByid($uid);
		$myInterest = array();
		$intScore = array();
		foreach(explode(" ",$difftags["userTags"]) as $k=>$atag){
			$index = 0;
			$diff = $difftags["diff"];
			foreach($diff as $k=>$friend){
				$ftags = explode(" ",$friend["tag"]);
				foreach($ftags as $k=>$ftag){
					if($atag==$ftag){
						$index++;
						if(!isset($myInterest[$atag]))
							$myInterest[$atag] =array();
						$hasit = false;
						foreach($top30 as $kt=>$vt){
							if($vt['fid']==$friend['fname']){
								$hasit = true;
								break;
							}
						}
						if(!$hasit)
							$myInterest[$atag][]=$friend;
					}
				}
			}
			if($atag!=""){
				$intScore[] = array("score"=>$index/count($diff),"tag"=>$atag);
			}
		}
		rsort($intScore);
		return $this->getMyMostInterest($uid,$intScore,$myInterest,$top30,$difftags['diff']);
	}

	private function getMyMostInterest($id,$intScore,$myInterest,$top30,$difftags){

		$friends = $this->GetFriendsByid($id);
		$fids = array();
		foreach($friends as $key=>$value){
			array_push($fids,$value["fid"]);
		}
		$ftags = $this->getTagByIds($fids);

		$s4 = array();
		$t4 = array();
		$leader = array();
		$tempallhasshow = array();
		$showedUser = array();
		$mymedia = array();

		for($i = 0; $i<4;$i++){
			$s4[] = $intScore[$i];
			$t4[$intScore[$i]['tag']] = $myInterest[$intScore[$i]['tag']];
		}

		foreach($s4 as $key=>$tag){
			$tempid = array();
			foreach($t4[$tag['tag']] as $key=>$tg){
				$hasit = false;
				foreach($ftags as $tkey=>$tvalue){
					if($tg['fid']==$tvalue['uid']){
						if($tvalue['type']==2)
							$hasit = true;
						break;
					}
				}
				if(!$hasit)
					$tempid[] = $tg['fname'];
				else
					$mymedia[] = $tg['fname'];
			}
			$tempid = array_diff($tempid,$tempallhasshow);
			rsort($tempid);
			$leader[$tag['tag']]=$this->getLeaderrank($id,$tempid);
			foreach($leader[$tag['tag']] as $key=>$val){
				$tempallhasshow[]= $val['fid'];
				$showedUser[] = $val['fid'];
			}

		}
		unset($s4);
		unset($t4);
		unset($friends);
		unset($tempallhasshow);
		foreach($ftags as $key=>$value){
			if($value['type']!="1"){
				$mymedia[] = $value['uname'];
				$showedUser[] = $value['uname'];
			}
		}
		foreach($top30 as $key=>$value)
			$showedUser[] = $value['fid'];
		$showedid = array();
		foreach($showedUser as $key=>$value){
			foreach($difftags as $k=>$v){
				if($v['fname']==$value){
					unset($difftags[$k]);
					break;
				}
			}
		}
		foreach($difftags as $key=>$value){
			$showedid[]=$value['fname'];
		}
		unset($showedUser);
		return array('interest'=>$leader,'media'=>$this->getLeaderrank($id,$mymedia,NULL,NULL),
					'other'=>$this->getLeaderrank($id,$showedid,NULL,NULL));
	}

	/*
	*string $id      用户UID
	*array  $fids    查询朋友ID
	*return array()  返回leaderrank前5
	*/
	function getLeaderrank($uid,$fids,$start=0,$limit=5){
		$sql = "select fid from wb_score where uid='".$uid."' and (";
		$tempfid = "";
		$fsum = count($fids);
		if($fsum==0)
			return null;
		for($index = 0 ; $index<$fsum;$index++){
			if($index!=($fsum-1)){
				$tempfid.="fid='".$fids[$index]."' or ";
			}else{
				$tempfid.="fid='".$fids[$index]."')";			
			}
		}
		if($limit!=null)
			$query = $this->db->query($sql.$tempfid."ORDER BY `leaderrank` DESC  LIMIT ".$start.",".$limit);
		else
			$query = $this->db->query($sql.$tempfid."ORDER BY `leaderrank` DESC");

		return $query->result_array();
	}
}
?>